import { defineStore } from 'pinia'

interface AppSettings {
  theme: 'light' | 'dark'
  language: string
  fontSize: number
  notifications: boolean
  [key: string]: any
}

export const useSettingsStore = defineStore('settings', {
  state: (): AppSettings => ({
    theme: 'light',
    language: 'zh-CN',
    fontSize: 14,
    notifications: true,
  }),

  actions: {
    // 更新设置
    updateSettings(settings: Partial<AppSettings>) {
      this.$patch(settings)
    },

    // 重置设置
    resetSettings() {
      this.$reset()
    },

    // 获取设置
    getSetting<T>(key: keyof AppSettings): T {
      return this[key] as T
    },

    // 设置单个值
    setSetting<T>(key: keyof AppSettings, value: T) {
      this[key] = value as any
    },
  },

  persist: {
    key: 'ECHO-SETTINGS',
    storage: {
      getItem: (key: string) => {
        return uni.getStorageSync(key)
      },
      setItem: (key: string, value: any) => {
        uni.setStorageSync(key, value)
      },
    },
  },
})
